package com.tz;

//import com.tz.crm.AppConfig4Test;

import com.tz.crm.AppConfig4Test;
import com.tz.crm.entity.Area;
import com.tz.crm.entity.Customer;
import com.tz.crm.entity.User;
import com.tz.crm.entity.enumeration.Level;
import com.tz.crm.entity.enumeration.Source;
import com.tz.crm.entity.enumeration.Status;
import com.tz.crm.entity.enumeration.Type;
import com.tz.crm.service.AreaService;
import com.tz.crm.service.CustomerService;
import com.tz.crm.service.UserService;
import org.fluttercode.datafactory.impl.DataFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.util.Calendar;
import java.util.List;

/**
 * 为首页 index 的 chart 图表插入数据
 */

@Test
@ContextConfiguration(classes = AppConfig4Test.class)
public class InitChartData extends AbstractTestNGSpringContextTests {

    @Autowired
    private AreaService areaService;
    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;

    @Test
    public void initNewCustomerData() {
        DataFactory dataFactory = new DataFactory();
        List<User> userList = userService.getAll();
        Level[] levels = Level.values();
        Source[] sources = Source.values();
        Status[] statuses = Status.values();
        List<Area> provinces = areaService.getByLevel(1);
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j <= i; j++) {
                Customer cus = new Customer();
                cus.setName("Tina");
                cus.setAddress("addr");
                cus.setAddress2("addr2");
                cus.setBusiness("");
                cus.setFax("");
                cus.setLevel(levels[dataFactory.getNumberBetween(0,
                        levels.length - 1)]);
                cus.setSource(sources[dataFactory.getNumberBetween(0,
                        sources.length - 1)]);
                cus.setStatus(statuses[dataFactory.getNumberBetween(0,
                        statuses.length - 1)]);
                cus.setOwner(userList.get(dataFactory.getNumberBetween(0,
                        userList.size() - 1)));
                cus.setWebSite("www."
                        + dataFactory.getRandomWord().toLowerCase() + ".com");
                cus.setType(Type.ENTERPRISE);
                String provinceName = provinces.get(
                        (int) (Math.random() * (provinces.size()))).getName();
                cus.setProvinceName(provinceName);
                Calendar createDate = Calendar.getInstance();
                createDate.set(createDate.get(Calendar.YEAR), createDate.get(Calendar.MONTH)
                        - j, 1, 24, 00);
                cus.setCreateDate(createDate.getTime());
                customerService.save(cus);
            }
        }
    }

}
